package com.apobates.jforum2.troll.accounts.biz.impl.api;

import com.apobates.jforum2.troll.accounts.biz.MemberActionDescriptor;
import com.apobates.jforum2.troll.accounts.entity.Member;
import com.apobates.jforum2.troll.regular.ActionOperatorLog;
import com.apobates.jforum2.troll.regular.ActionOperatorLogProvider;
import com.apobates.jforum2.troll.regular.ForumActionEnum;
import com.apobates.jforum2.troll.regular.event.MemberActionLog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;

/**
 * 会员操作日志消息提供者
 * @author xiaofanku
 * @since 20210925
 */
@Component
public class MemberActionLogStreamProvider implements ActionOperatorLogProvider {
    @Autowired
    private StreamBridge streamBridge;
    private final static Logger logger = LoggerFactory.getLogger(MemberActionLogStreamProvider.class);

    @Override
    public void send(ActionOperatorLog log){
        logger.error("[LOG][Account]start send");
        streamBridge.send(log.getBindingNames()+"-out-0", log.getPayload());
        logger.error("[LOG][Account]send finish");
    }

    public MemberActionLog action(ForumActionEnum action, Member member, MemberActionDescriptor descriptor, boolean status){
        MemberActionLog bal = new MemberActionLog();
        bal.setAction(action);
        bal.setMemberId(member.getId());
        bal.setMemberNickname(member.getNickname());
        bal.setIpAddr(descriptor.location().getIpAddr());
        bal.setRefererURL(descriptor.getReferrer());
        bal.setDevice(descriptor.getDevice());
        bal.setToken(descriptor.getToken());
        bal.setSuccess(status);
        bal.setDateTime(LocalDateTime.now());
        this.send(bal);
        return bal;
    }
}
